home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 25 / AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso / PublicDomain / Spiele / TextElite / TextElite.c < prev    next >
C/C++ Source or Header  |  2000-02-27  |  26KB  |  977 lines

  1. /*
  2.  * Textual version of Elite trading (C implementation)
  3.  * Converted by Ian Bell from 6502 Elite sources.
  4.  * Original 6502 Elite by Ian Bell & David Braben.
  5.  *
  6.  * Modified by Duane McDonnell (TextElite.c) based upon original
  7.  * txtelite.c code by Ian Bell. Changes made:
  8.  *
  9.  * - Added commander load, save and reset commands.
  10.  * - Extended government and economy names.
  11.  * - Increased maximum string length to 128 chars (for filenames).
  12.  * - parser() no longer destroys the input string. Pressing enter
  13.  *   on its own now brings up the help menu (rather than buying food).
  14.  * - 'Local' command now tells you which systems are in range (IR)
  15.  *   or out of range (OR) according to your current fuel load.
  16.  * - Text output tweaked a bit (more verbose, columns line up etc).
  17.  *
  18.  * Second release version, corrects a few problems. The first version
  19.  * didn't have a version number, this one is numbered 1.1:
  20.  *
  21.  * - Hold status is now accurate. First version didn't account for
  22.  *   different units (all goods were calculated as tonnes).
  23.  * - dosave() will now delete partial saves in the event of a write error.
  24.  * - Embedded an Amiga version string in the code.
  25.  * - Amiga executable now compiled with stack extension on.
  26.  */
  27.  
  28. /* ----------------------------------------------------------------------
  29.   The nature of basic mechanisms used to generate the Elite socio-economic
  30. universe are now widely known. A competant games programmer should be able to
  31. produce equivalent functionality. A competant hacker should be able to lift 
  32. the exact system from the object code base of official conversions.
  33.  
  34.   This file may be regarded as defining the Classic Elite universe.
  35.  
  36.   It contains a C implementation of the precise 6502 algorithms used in the
  37.  original BBC Micro version of Acornsoft Elite together with a parsed textual
  38.  command testbed.
  39.  
  40.   Note that this is not the universe of David Braben's 'Frontier' series.
  41.  
  42.  
  43. ICGB 13/10/99
  44. iancgbell@email.com
  45. www.ibell.co.uk
  46.   ---------------------------------------------------------------------- */
  47.  
  48.  
  49. /* Note that this program is "quick-hack" text parser-driven version
  50. of Elite with no combat or missions.
  51. */
  52.  
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <time.h>
  57. #include <math.h>
  58.  
  59. char ver[] = "$VER: TextElite 1.1 (27.2.2000)\r\n\0";
  60.  
  61. #define true (-1)
  62. #define false (0)
  63. #define tonnes (0)
  64.  
  65. #define maxlen (128) /* Length of strings */
  66.  
  67. typedef int boolean;
  68. typedef unsigned short uint16;
  69. typedef signed short int16;
  70. typedef signed long int32;
  71.  
  72. typedef uint16 uint;
  73.  
  74. typedef int planetnum;
  75.  
  76. typedef struct
  77. { uint16 w0;
  78.   uint16 w1;
  79.   uint16 w2;
  80. } seedtype;  /* six byte random number used as seed for planets */
  81.  
  82. typedef struct
  83. {     uint x;
  84.    uint y;       /* One byte unsigned */
  85.    uint economy; /* These two are actually only 0-7  */
  86.    uint govtype;   
  87.    uint techlev; /* 0-16 i think */
  88.    uint population;   /* One byte */
  89.    uint productivity; /* Two byte */
  90.    uint radius; /* Two byte (not used by game at all) */
  91.    char name[12];
  92. } plansys ;
  93.  
  94. #define galsize (256)
  95. #define AlienItems (16)
  96. #define lasttrade AlienItems
  97.  
  98. #define numforLave 7       /* Lave is 7th generated planet in galaxy one */
  99. #define numforZaonce 129
  100. #define numforDiso 147
  101. #define numforRied 46
  102.  
  103. plansys galaxy[galsize]; /* Need 0 to galsize-1 inclusive */
  104.  
  105. seedtype seed;
  106.  
  107. typedef struct
  108. {                         /* In 6502 version these were: */
  109.    uint baseprice;        /* one byte */
  110.    int16 gradient;   /* five bits plus sign */
  111.    uint basequant;        /* one byte */
  112.    uint maskbyte;         /* one byte */
  113.    uint units;            /* two bits */
  114.    char   name[20];         /* longest="Radioactives" */
  115.   } tradegood ;
  116.  
  117.  
  118. typedef struct
  119. {    uint quantity[lasttrade+1];
  120.   uint price[lasttrade+1];
  121. } markettype ;
  122.  
  123. /* Player workspace */
  124. uint     shipshold[lasttrade+1];  /* Contents of cargo bay */
  125. planetnum  currentplanet;           /* Current planet */
  126. uint     galaxynum;               /* Galaxy number (1-8) */
  127. int32    cash;
  128. uint     fuel;
  129. markettype localmarket;
  130. uint     holdspace;
  131. uint     holdtotal;
  132.  
  133. int fuelcost =2; /* 0.2 CR/Light year */
  134. int maxfuel =70; /* 7.0 LY tank */
  135.  
  136. const uint16 base0=0x5A4A;
  137. const uint16 base1=0x0248;
  138. const uint16 base2=0xB753;  /* Base seed for galaxy 1 */
  139.  
  140. char pairs[] = "..LEXEGEZACEBISO"
  141.                "USESARMAINDIREA."
  142.                "ERATENBERALAVETI"
  143.                "EDORQUANTEISRION"; /* Dots should be nullprint characters */
  144.  
  145. char govnames[][maxlen]={"Anarchy         ",
  146.                          "Feudal          ",
  147.                          "Multi-Government",
  148.                          "Dictatorship    ",
  149.                          "Communist       ",
  150.                          "Confederacy     ",
  151.                          "Democracy       ",
  152.                          "Corporate State "};
  153.  
  154. char econnames[][maxlen]={"Rich Industrial     ",
  155.                           "Average Industrial  ",
  156.                           "Poor Industrial     ",
  157.                           "Mainly Industrial   ",
  158.                           "Mainly Agricultural ",
  159.                           "Rich Agricultural   ",
  160.                           "Average Agricultural",
  161.                           "Poor Agricultural   "};
  162.  
  163. char unitnames[][5] ={"t","kg","g"};
  164.  
  165. /* Data for DB's price/availability generation system */
  166. /*                   Base  Grad Base Mask Un   Name
  167.                      price ient quant     it              */ 
  168.  
  169. #define POLITICALLY_CORRECT    0
  170. /* Set to 1 for NES-sanitised trade goods */
  171.  
  172. tradegood commodities[]=
  173.                    {
  174.                     {0x13,-0x02,0x06,0x01,0,"Food        "},
  175.                     {0x14,-0x01,0x0A,0x03,0,"Textiles    "},
  176.                     {0x41,-0x03,0x02,0x07,0,"Radioactives"},
  177. #if POLITICALLY_CORRECT
  178.                     {0x28,-0x05,0xE2,0x1F,0,"Robot Slaves"},
  179.                     {0x53,-0x05,0xFB,0x0F,0,"Beverages   "},
  180. #else
  181.                     {0x28,-0x05,0xE2,0x1F,0,"Slaves      "},
  182.                     {0x53,-0x05,0xFB,0x0F,0,"Liquor/Wines"},
  183. #endif 
  184.                     {0xC4,+0x08,0x36,0x03,0,"Luxuries    "},
  185. #if POLITICALLY_CORRECT
  186.                     {0xEB,+0x1D,0x08,0x78,0,"Rare Species"},
  187. #else
  188.                     {0xEB,+0x1D,0x08,0x78,0,"Narcotics   "},
  189. #endif 
  190.                     {0x9A,+0x0E,0x38,0x03,0,"Computers   "},
  191.                     {0x75,+0x06,0x28,0x07,0,"Machinery   "},
  192.                     {0x4E,+0x01,0x11,0x1F,0,"Alloys      "},
  193.                     {0x7C,+0x0d,0x1D,0x07,0,"Firearms    "},
  194.                     {0xB0,-0x09,0xDC,0x3F,0,"Furs        "},
  195.                     {0x20,-0x01,0x35,0x03,0,"Minerals    "},
  196.                     {0x61,-0x01,0x42,0x07,1,"Gold        "},
  197.                     {0xAB,-0x02,0x37,0x1F,1,"Platinum    "},
  198.                     {0x2D,-0x01,0xFA,0x0F,2,"Gem-Stones  "},
  199.                     {0x35,+0x0F,0xC0,0x07,0,"Alien Items "},
  200.                    };
  201.  
  202. /**-Required data for text interface **/
  203. char tradnames[lasttrade][maxlen]; /* Tradegood names used in text commands
  204.                                       Set using commodities array */
  205.  
  206. boolean parser(char *s);
  207.  
  208. #define nocomms 16
  209.  
  210. boolean dobuy(char *);
  211. boolean dosell(char *);
  212. boolean dofuel(char *);
  213. boolean dojump(char *);
  214. boolean docash(char *);
  215. boolean domkt(char *);
  216. boolean dohelp(char *);
  217. boolean dohold(char *);
  218. boolean dosneak(char *);
  219. boolean dolocal(char *);
  220. boolean doinfo(char *);
  221. boolean dogalhyp(char *);
  222. boolean doload(char *);
  223. boolean dosave(char *);
  224. boolean doreset(char *);
  225. boolean doquit(char *);
  226.  
  227. char commands[nocomms][maxlen]=
  228.   {"buy",        "sell",     "fuel",     "jump",
  229.    "cash",       "mkt",      "help",     "hold",
  230.    "sneak",      "local",    "info",     "galhyp",
  231.    "load",       "save",     "!reset",    "quit"
  232.   };
  233.  
  234. boolean (*comfuncs[nocomms])(char *)=
  235.    {dobuy,         dosell,       dofuel,    dojump,
  236.     docash,        domkt,        dohelp,    dohold,
  237.     dosneak,       dolocal,      doinfo,    dogalhyp,
  238.     doload,        dosave,       doreset,   doquit
  239.   };  
  240.  
  241. /**- General functions **/
  242.  
  243. char randbyte(void)    { return (char)(rand()&0xFF);}
  244.  
  245. uint mymin(uint a,uint b) { if(a<b) return(a);    else return(b);}
  246.  
  247. void stop(char * string)
  248. { printf("\n%s",string);
  249.   exit(1);
  250. }
  251.  
  252.  /**+  ftoi **